import axios from "axios";
import { message } from "ant-design-vue";

axios.defaults.headers["Content-Type"] = "application/json;charset=UTF-8";
// 创建axios实例
const service = axios.create({
  // axios中请求配置有baseURL选项，表示请求URL公共部分
    baseURL: "http://localhost:8888/codes",
  //  baseURL: "http://120.78.148.181:8888/codes",
  // 超时
  timeout: 10000,
});
// request拦截器
service.interceptors.request.use(
  (config) => {
    // 是否需要设置 token
    return config;
  },
  (error) => {
    console.log(error);
    Promise.reject(error);
  }
);

// 响应拦截器
service.interceptors.response.use(
  (success) => {
    if (success.status == 200 && success.data.code == 200) {
      //message.success(  success.data.message ,3);
    }
    else {
      message.error(success.data.message, 5);
      return;
    }
    return success.data;
  },
  (error) => {
    if (error.response.status == 201 || error.response.status == 404) {
      message.error("服务器被吃了( ╯□╰ )", 5);
    } else if (error.response.status == 403) {
      message.error("权限不足，请联系管理员", 5);
    } else {
      if (error.response.data.message) {
        message.error(error.response.data.message, 5);
      } else {
        message.error("未知错误!", 5);
      }
    }
    return Promise.reject(error);
  }
);

export default service;